package com.hotacorp.opencloud.userservice.controller;

import java.util.Arrays;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hotacorp.opencloud.common.authcommon.SysClientBean;
import com.hotacorp.opencloud.common.commdata.Result;
import com.hotacorp.opencloud.common.log.annotation.LogAnnotation;
import com.hotacorp.opencloud.common.mybatisplus.PageData;
import com.hotacorp.opencloud.common.resourcecommon.validator.group.AddGroup;
import com.hotacorp.opencloud.common.resourcecommon.validator.group.UpdateGroup;
import com.hotacorp.opencloud.userservice.entity.OauthClientDetailsEntity;
import com.hotacorp.opencloud.userservice.service.OauthClientDetailsService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 应用管理
 *
 * @author lwg
 * @email lwg@gmail.com
 * @date 2019-12-25 11:52:19
 */
@RestController
@Api(tags = "应用管理API")
public class OauthClientDetailsController {
    @Autowired
    private OauthClientDetailsService oauthClientDetailsService;

    /**
     * 列表
     */
    @GetMapping(value = "/oauthclientdetails/list")
    @PreAuthorize("hasAuthority('userservice:oauthclientdetails:list')")
	@ApiOperation(value = "查询应用")
    @LogAnnotation(module = "应用")
    public Result<?> list(@RequestParam Map<String, Object> params){
        PageData page = oauthClientDetailsService.queryPage(params);

        return Result.succeed("succeed", page);
    }


    /**
     * 信息
     */
    @GetMapping(value = "/oauthclientdetails/info/{id}")
    @PreAuthorize("hasAuthority('userservice:oauthclientdetails:info')")
	@ApiOperation(value = "应用详情")
    @LogAnnotation(module = "应用")
    public Result<?> info(@PathVariable("id") Integer id){
		OauthClientDetailsEntity oauthClientDetails = oauthClientDetailsService.getById(id);

        return Result.succeed("succeed", oauthClientDetails);
    }

    /**
     * 保存
     */
    @PostMapping(value = "/oauthclientdetails/save")
    @PreAuthorize("hasAuthority('userservice:oauthclientdetails:save')")
	@ApiOperation(value = "添加应用")
    @LogAnnotation(module = "应用")
    public Result<?> save(@RequestBody @Validated(AddGroup.class) OauthClientDetailsEntity oauthClientDetails){
		oauthClientDetailsService.saveClient(oauthClientDetails);

        return Result.succeed();
    }

    /**
     * 修改
     */
    @PostMapping(value = "/oauthclientdetails/update")
    @PreAuthorize("hasAuthority('userservice:oauthclientdetails:update')")
	@ApiOperation(value = "修改应用")
    @LogAnnotation(module = "应用")
    public Result<?> update(@RequestBody @Validated(UpdateGroup.class) OauthClientDetailsEntity oauthClientDetails){
		oauthClientDetailsService.updateClient(oauthClientDetails);

        return Result.succeed();
    }

    /**
     * 删除
     */
    @PostMapping(value = "/oauthclientdetails/delete")
    @PreAuthorize("hasAuthority('userservice:oauthclientdetails:delete')")
	@ApiOperation(value = "删除应用")
    @LogAnnotation(module = "应用")
    public Result<?> delete(@RequestBody Integer[] ids){
		oauthClientDetailsService.removeByIds(Arrays.asList(ids));

        return Result.succeed();
    }

    @GetMapping(value = "/anon/client", params = "clientId")
    @ApiOperation(value = "根据clientid查询应用")
    @ApiIgnore
    public Result<?> loadClientByClientId(@RequestParam("clientId") String clientId) {
    	SysClientBean ddd = oauthClientDetailsService.loadClientByClientId(clientId);
    	if (ddd == null) {
    		return Result.failed("没有找到应用");
    	} else {
        	return Result.succeed("succeed", ddd);
		}
    }

}
